import 'package:chop/Model/POLI_entity.dart';
import 'package:chop/Model/order_entity.dart';
import 'package:chop/common/DioUtils.dart';
import 'package:chop/common/MessageUtils.dart';
import 'package:chop/view/pay/poli_pay.dart';
import 'package:chop/widgets/TextSizeWidget.dart';
import 'package:chop/widgets/ToastWidget.dart';
import 'package:chop/widgets/appBar.dart';
import 'package:chop/widgets/loadingDialog.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:loading_animations/loading_animations.dart';

class PayList extends StatefulWidget {
  final orderData orderdata;

  const PayList({Key key, this.orderdata}) : super(key: key);
  @override
  _PayListState createState() => _PayListState();
}

class _PayListState extends State<PayList> {
  bool isNetWorkBad = true;
  String pay = "0";
  orderData data;
  var flutterWebviewPlugin = MsgUtils.getWebView();
  void initState() {
    // TODO: implement initState
    super.initState();
    this.data = widget.orderdata;
  }

  Widget getBody() {
    if (isNetWorkBad) {
      // ignore: missing_return
      return _buildBody();
    } else {
      return Center(
        child: LoadingJumpingLine.square(),
      );
    }
  }

  Widget _buildBody() {
    return Stack(
      children: <Widget>[
        ListView(
          children: <Widget>[
            Container(
              margin: EdgeInsets.fromLTRB(10,5,10,5),
              padding: EdgeInsets.fromLTRB(10,5,10,5),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  color: Colors.white
              ),
//          width: double.infinity,
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Radio(
                      value: "0",
                      groupValue: pay,
                      onChanged: (val) {
                        setState(() {
                          pay = val;
                        });
                        print("选择支付方式为：${val}");
                      },),
                  ),
                  Expanded(
                      flex: 2,
                      child: Image(
                        image: AssetImage("asset/pay/POLiLogo-43.png"),
                      )),
                  Expanded(
                    flex: 7,
                    child: FixedSizeText(""),
                  )
                ],
              ),
            ),
            Container(
//          width: double.infinity,
              margin: EdgeInsets.fromLTRB(10,5,10,5),
              padding: EdgeInsets.fromLTRB(10,5,10,5),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  color: Colors.white
              ),
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Radio(
                        value: "1",
                        groupValue: pay,
                        onChanged: (val) {
                          setState(() {
                            pay = val;
                          });
                          print("选择支付方式为：${val}");
                        }),
                  ),
                  Expanded(
                      flex: 2,
                      child: Image(
                        image: AssetImage("asset/pay/zfb.png"),
                      )),
                  Expanded(
                    flex: 7,
                    child: FixedSizeText(""),
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.only(top: 10,bottom: 10),
              margin: EdgeInsets.only(left: 50,right: 50),
              child: RaisedButton(
                //提交按钮
                color: Colors.lightBlueAccent,
                highlightColor: Colors.blueAccent,
                splashColor: Colors.blue,
                colorBrightness: Brightness.dark,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10)),
                onPressed: () async {
//                Get.to(PayList());
                if(pay == "1"){
                  return Toast.toast(context,position: ToastPostion.bottom,bgColor: Colors.redAccent,msg: "暂不支持该方式支付");
                }
                showDialog<Null>(
                  context: context, //BuildContext对象
                  barrierDismissible: false,
                  builder: (BuildContext context) {
                    return LoadingDialog(
                      //调用对话框
                      text: 'Loading...',
                    );
                  },
                );
                 await DioUtils.sendPOLI(data).then((POLIEntity poliEntity){
                   if(poliEntity.success){
                     Get.back();
                     Get.to(PoliPay(url: poliEntity.navigateURL,));
                   }
                 });
                },
                child: FixedSizeText(
                  '立即支付',
                  style:
                  TextStyle(color: Colors.white, fontSize: 15),
                ),
              ),
            ),
          ],
        ),
//        Positioned(
//          bottom: ,
//          width: MediaQuery.of(context).size.width,
//          child:
//        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BaseViewBar(
        childView: BaseTitleBar("支付方式"),
        preferredSize: Size.fromHeight(50.00),
      ),
      body: getBody(),

    );
  }
}
